# -*- coding: utf-8 -*-
import datetime

from odoo import fields, models, api


class SerialNumber(models.Model):
    _inherit = 'ir.sequence'

    is_serial_number = fields.Boolean(string="Serial Number", default=False, required=True)

    def get_serial_number(self, code='', prefix=False, padding=3, number_next=1, number_increment=1):
        """
        生成序列号（通过字段'is_serial_number'与odoo原生序列区分）
        ;code默认为'default_serial_number'(建议自定义为’模型名.字段名‘e.g；product_template.default_code，方便区分)
        ;prefix默认为'年月日'
        ;padding默认为3
        ;number_next默认为1
        ;number_increment默认为1
        """
        if not code:
            code = 'default_serial_number'
        if not prefix and prefix != '':
            prefix = '%(year)s%(month)s%(day)s'
        # domain = [
        #     ('code', '=', code), ('prefix', '=', prefix), ('padding', '=', padding), ('is_serial_number', '=', True),
        #     ('number_next', '=', number_next), ('number_increment', '=', number_increment)
        # ]
        domain = [
            ('code', '=', code), ('prefix', '=', prefix), ('padding', '=', padding), ('is_serial_number', '=', True),
            ('number_increment', '=', number_increment)
        ]
        serial_id = self.sudo().search(domain)
        if not serial_id:
            serial_id = self.sudo().create({
                'name': code,
                'code': code,
                'is_serial_number': True,
                'prefix': prefix,
                'padding': padding,
                'number_next': number_next,
                'number_increment': number_increment,
            })
        return serial_id._next()
